package com.baizhiedu.basic.controller;

import com.baizhiedu.basic.aop.Target;
import com.baizhiedu.basic.aop.TargetInterface;
import com.baizhiedu.basic.pojo.Fighter;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
import com.baizhiedu.basic.pojo.Person;
import com.baizhiedu.basic.pojo.vo.VO;
import com.baizhiedu.basic.service.AnnoService;
import com.baizhiedu.basic.service.ServiceInterface;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/9
 */
@Slf4j
@Controller
@RequestMapping("/user")
public class FighterController {

    @Autowired
    private ServiceInterface service;

    //    页面跳转
//    方式1：直接通过String返回页面
    @RequestMapping(value = "/quick", method = RequestMethod.GET, params = {"username!=root", "passwd", "againPasswd"})
    public String save(String name, String passwd, @RequestParam(name = "againPasswd") String ap) {
        System.out.println("Controller save running...");
        System.out.println("annoService instance: " + service.getClass());
        System.out.println("username:" + name);
        System.out.println("passwd:" + passwd);
        System.out.println("againPasswd:" + ap);
        return "success";
    }

    //    方式2：ModelAndView
    @RequestMapping(value = "/quick2", method = RequestMethod.GET)
    public ModelAndView save2() {
        System.out.println("Controller save running...");
        ModelAndView mv = new ModelAndView();
        mv.addObject("username", "Robert.Song");
        mv.addObject("img", new File("imgs/Snipaste_2021-10-28_07-38-58.png"));
        mv.setViewName("index");
        return mv;
    }

    //    另一种引用ModelAndView的方式
    @RequestMapping(value = "/quick3", method = RequestMethod.GET)
    public ModelAndView save3(ModelAndView mv) {
        System.out.println("Controller save running...");
        mv.addObject("username", "Robert.Song");
        mv.setViewName("index");
        return mv;
    }

    //    方式3：引用Model填充数据，通过String返回页面,视图渲染时会将model渲染到待返回页面
    @RequestMapping(value = "/quick4", method = RequestMethod.GET)
    public String save4(Model m) {
        System.out.println("Controller save running...");
        m.addAttribute("username", "Robert.Song");
        return "index";
    }

    //  方式4：直接使用request，跨过model的封装
    @RequestMapping(value = "/quick5", method = RequestMethod.GET)
    public String save5(HttpServletRequest request) {
        request.setAttribute("username", "Robert.Song");
        return "index";
    }

    //    回写数据
    @RequestMapping(value = "/quick6", method = RequestMethod.GET)
    public void save6(HttpServletResponse response) {
        try {
            response.getWriter().print("hello Robert.Song");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/quick7", method = RequestMethod.GET)
    public String save7() {
        return "hello Robert.Song";
    }

    @ResponseBody
    @RequestMapping(value = "/quick8", method = RequestMethod.GET)
    public String save8() {
        return "{\"title\":\"hello\",\"name\":\"宋亚辉\"}";
    }

    @ResponseBody
    @RequestMapping(value = "/quick9", method = RequestMethod.GET)
    public String save9() throws JsonProcessingException {
        Person p = new Person();
        p.setAge(20);
        p.setName("宋亚辉");
        p.setPassword("961003");
//        使用json工具转换为json格式字符串返回
        ObjectMapper objcectMapper = new ObjectMapper();
        String json = objcectMapper.writeValueAsString(p);
        return json;
    }

    @ResponseBody
    @RequestMapping(value = "/quick10", method = RequestMethod.GET)
    public Person save10() {
        Person p = new Person();
        p.setAge(20);
        p.setName("宋亚辉");
        p.setPassword("961003");
        return p;
    }

    @RequestMapping(value = "quick11", method = RequestMethod.GET)
    @ResponseBody
    public void save11(String username, int age) {
        System.out.println(username);
        System.out.println(age);
    }

    @RequestMapping(value = "quick12", method = RequestMethod.GET)
    @ResponseBody
    public void save12(Person person) {
        System.out.println(person);
    }

    @RequestMapping(value = "quick13", method = RequestMethod.GET)
    @ResponseBody
    public void save13(String[] strings) {
        System.out.println(Arrays.asList(strings));
    }

    @RequestMapping(value = "quick14", method = RequestMethod.POST)
    @ResponseBody
    public void save14(VO vo) {
        System.out.println(vo.toString());
    }

    @RequestMapping(value = "quick14_2")
    @ResponseBody
    public String save14_2(@RequestParam(name = "file") MultipartFile files[], @RequestParam(name = "name", required = false) String name, @RequestParam(name = "passwd") String passwd) throws IOException {
        for (MultipartFile multipartFile : files) {
            String originalFilename = multipartFile.getOriginalFilename();
            System.out.println(originalFilename);
            multipartFile.transferTo(new File("C:\\upload\\" + originalFilename));
        }
        System.out.println(name + "\r\n" + passwd + "\r\n" + files.length);
        return "{\"name\":" + name + ",\"passwd\":" + passwd + "}";
    }

    @RequestMapping(value = "quick15")
    @ResponseBody
    public List<Person> save15(@RequestBody List<Person> list) {
        System.out.println(list);
        return list;
    }

    @RequestMapping(value = "quick15_2")
    @ResponseBody
    public Person save15_2(@RequestParam(value = "name", required = true) String userName, Person person) {
        System.out.println(userName);
        System.out.println(person);
        return person;
    }

    //同时接收header和body
    @RequestMapping(value = "quick16/{name}")
    public void save16(@PathVariable String name, Person person) {
        System.out.println(name);
        System.out.println(person);
    }

    @RequestMapping(value = "quick17")
    @ResponseBody
    public String save17(@RequestHeader(value = "Host") String host, @RequestHeader(value = "User-Agent") String userAgent, @CookieValue(value = "JSESSIONID") String sessionId) {
        System.out.println(host);
        System.out.println(userAgent);
        System.out.println(sessionId);
        return sessionId;
    }

    @DateTimeFormat
    @RequestMapping(value = "/quick18")
    @ResponseBody
    public void save18(Date date) {
        System.out.println(date);
//        return date;
    }

    @RequestMapping(value = "/quick19")
    @ResponseBody
    public void quick19(String username, MultipartFile[] upload) {
        System.out.println(upload[0] instanceof MultipartFile);
        System.out.println(username);

        System.out.println(upload);
        new StringBuilder("teswt").toString();
    }

    @GetMapping("/quick20")
    public ModelAndView quick20(ModelAndView mv) {
        mv.addObject("name", "interceptor test");
        mv.setViewName("interceptor");
        return mv;
    }

    @GetMapping("/quick21")
    public String quick21() throws NullPointerException {
        Integer i = null;
        System.out.println(i.intValue());
        return "error";
    }

    @Autowired
    private TargetInterface target;

    @Autowired
    private Person person;

    @RequestMapping("/quick22")
    public String quick22() {
        target.save();
        log.info("person：" + person);
        return "index";
    }
}
